import store from "@/store"
import $ from "jquery"

function Console(key) {
  console.log(key)
}

function JSONStringify(item) {
  var cache = []
  var str = JSON.stringify(item, function (key, value) {
    if (typeof value === "object" && value !== null) {
      if (cache.indexOf(value) !== -1) {
        // 找到循环引用，放弃密钥
        return
      }
      // 在收藏中储存值
      cache.push(value)
    }
    return value
  })
  cache = null
}

function delay(time) {
  return new Promise(resolve => {
    setInterval(() => {
      resolve()
    }, time)
  })
}
// 通过name,id,type查找节点
function findNodesByKey(val, key) {
  let metaObj = store.state.myViewer.metaScene.metaObjects
  let searchList = []
  if (key != "name" && key != "id" && key != "type") {
    return "key值只允许name、id和type"
  }
  for (let i in metaObj) {
    if (key == "id") {
      // 区分大小写
      if (metaObj[i].id && metaObj[i].id.includes(val)) {
        searchList.push({
          id: metaObj[i].id,
          name: metaObj[i].name,
          type: metaObj[i].type
        })
      }
    } else {
      // 不区分大小写
      if (metaObj[i][key] && metaObj[i][key].toLowerCase().includes(val.toLowerCase())) {
        searchList.push({
          id: metaObj[i].id,
          name: metaObj[i].name,
          type: metaObj[i].type
        })
      }
    }
  }
  return searchList
}
// 节流
function throttle(fn, time) {
  let lastTime = 0
  time = time || 10
  return function () {
    let now = new Date().getTime()
    if (now - lastTime > time) {
      fn.apply(this, arguments)
      lastTime = now
    }
  }
}
// 移动元素方法
function moveDiv(id) {

  //   目标元素
  var moveEle = $(`#${id} .title`)
  moveEle.on("mousedown", e => {
    // 鼠标与DIV的偏移量
    var startX = e.pageX - moveEle.offset().left
    var startY = e.pageY - moveEle.offset().top
    $(document).on("mousemove", throttle(move, 10))

    function move(e) {
      //debugger
      var endX = e.pageX - startX
      var endY = e.pageY - startY
      // 超出屏幕左边界
      if (endX <= 0) {
        endX = 0
      }
      // 超出屏幕上边界
      if (endY <= 0) {
        endY = 0
      }
      // 超出屏幕右边界
      if (endX >= window.innerWidth - moveEle.parents(`#${id}`)[0].offsetWidth) {
        endX = window.innerWidth - moveEle.parents(`#${id}`)[0].offsetWidth
      }
      // 超出屏幕下边界
      if (endY >= window.innerHeight - moveEle.parents(`#${id}`)[0].offsetHeight) {
        endY = window.innerHeight - moveEle.parents(`#${id}`)[0].offsetHeight
      }
      moveEle.parents(`#${id}`).css({
        left: endX
      })
      moveEle.parents(`#${id}`).css({
        top: endY
      })
    }
    $(document).on("mouseup", () => {
      $(document).off("mousemove")
    })
  })
}
// n 次数， r 半径， s步长
function getPos(n, r, s) {
  if (n % 8 != 1) {}
  r = r + s * Math.floor(n / 9)
  // x左右，z前后
  let x, z
  // 8个点，45°
  var temp = Math.sqrt((r * r) / 2)
  switch (n % 8) {
    case 0:
      x = -temp
      z = temp
      break
    case 1:
      x = 0
      z = r
      break
    case 2:
      x = temp
      z = temp
      break
    case 3:
      x = r
      z = 0
      break
    case 4:
      x = temp
      z = -temp
      break
    case 5:
      x = 0
      z = -r
      break
    case 6:
      x = -temp
      z = -temp
      break
    case 7:
      x = -r
      z = 0
      break
  }
  return [x, 0, z]
}

function getRanColor() {
  let letters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]

  function getRanNum() {
    return Math.floor(Math.random() * 16)
  }
  let color = ""
  for (let i = 0; i < 6; i++) {
    color += letters[getRanNum()]
  }
  return `#${color}`
}


export default {
  Console,
  delay,
  findNodesByKey,
  moveDiv,
  getPos,
  getRanColor
}
